package cn.yunyichina.scheduler.jobs.entity.register;

import cn.yunyichina.scheduler.jobs.common.jackson.Jackson;
import cn.yunyichina.scheduler.jobs.constant.BizConstant;
import cn.yunyichina.scheduler.jobs.entity.vo.MessagePushParamsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

public class RegisterRecord extends Record {


    private Integer regStatus;


    private Long registerTime;
    //不存入数据库
    private String registerDate;


    private Integer isException;


    private Integer isHandleSuccess;

 
    private Integer handleCount;


    private String handleLog;


    private Integer regType;


    private Long payTimeoutTime;


    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date scheduleDate;

 
    private String scheduleDateLable;


    private Integer timeFlag;

  
    @DateTimeFormat(pattern = "HH:mm")
    private Date beginTime;

 
    @DateTimeFormat(pattern = "HH:mm")
    private Date endTime;

   
    private Integer regPersonType;

 
    private Integer regMode;


    private String serialNum;


    private String visitLocation;


    private String barcode;


    private String visitDesc;


    private String diseaseDesc;


    private Integer onlinePaymentType;


    private Integer waitPayTime;


    private Integer isValid;


    private String deptCode;


    private String deptName;


    private Integer category;


    private String doctorCode;


    private String doctorName;


    private String doctorTitle;


    private Integer regFee;


    private Integer treatFee;


    private Integer realRegFee;

 
    private Integer realTreatFee;

 
    protected Double refundFee;


    protected Integer isPay;

  
    private String tradeType;


    private String tradeTime;



    private String cancelSerialNo;

    private String cancelBillNo;


    private String reserveOrderNo;

    public String getReserveOrderNo() {
        return reserveOrderNo;
    }

    public void setReserveOrderNo(String reserveOrderNo) {
        this.reserveOrderNo = reserveOrderNo;
    }

    public String getCancelSerialNo() {
        return cancelSerialNo;
    }

    public void setCancelSerialNo(String cancelSerialNo) {
        this.cancelSerialNo = cancelSerialNo;
    }

    public String getCancelBillNo() {
        return cancelBillNo;
    }

    public void setCancelBillNo(String cancelBillNo) {
        this.cancelBillNo = cancelBillNo;
    }

    private int insurance;
    private String tradeNo;
    private int totalFee;
    private String serialyb;
    private String bill;

    public String getSerialyb() {
        return serialyb;
    }

    public void setSerialyb(String serialyb) {
        this.serialyb = serialyb;
    }

    public String getBill() {
        return bill;
    }

    public void setBill(String bill) {
        this.bill = bill;
    }

    public int getInsurance() {
        return insurance;
    }

    public void setInsurance(int insurance) {
        this.insurance = insurance;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public int getTotalFee() {
        return totalFee;
    }

    public void setTotalFee(int totalFee) {
        this.totalFee = totalFee;
    }


    private Integer sStreatFee;

    public Integer getsStreatFee() {
        return sStreatFee;
    }

    public void setsStreatFee(Integer sStreatFee) {
        this.sStreatFee = sStreatFee;
    }



    private String paymentItems;


    private String dnh;

    public String getDnh() {
        return dnh;
    }

    public void setDnh(String dnh) {
        this.dnh = dnh;
    }

    public String getInsuredType() {
        return insuredType;
    }

    public void setInsuredType(String insuredType) {
        this.insuredType = insuredType;
    }

    public String getPatientType() {
        return patientType;
    }

    public void setPatientType(String patientType) {
        this.patientType = patientType;
    }

    public String getPaymentItems() {
        return paymentItems;
    }

    public void setPaymentItems(String paymentItems) {
        this.paymentItems = paymentItems;
    }


    private String workId;

    public String getDeptCode() {
        return deptCode;
    }

    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Integer getCategory() {
        return category;
    }

    public void setCategory(Integer category) {
        this.category = category;
    }

    public String getDoctorCode() {
        return doctorCode;
    }

    public void setDoctorCode(String doctorCode) {
        this.doctorCode = doctorCode;
    }

    public String getDoctorName() {
        return doctorName;
    }

    public void setDoctorName(String doctorName) {
        this.doctorName = doctorName;
    }

    public String getDoctorTitle() {
        return doctorTitle;
    }

    public void setDoctorTitle(String doctorTitle) {
        this.doctorTitle = doctorTitle;
    }

    public Integer getRegFee() {
        if (regFee == null) {
            regFee = 0;
        }
        return regFee;
    }

    public void setRegFee(Integer regFee) {
        this.regFee = regFee;
    }

    public Integer getTreatFee() {
        if (treatFee == null) {
            treatFee = 0;
        }
        return treatFee;
    }

    public void setTreatFee(Integer treatFee) {
        this.treatFee = treatFee;
    }

    public Integer getRealRegFee() {
        if (realRegFee == null) {
            realRegFee = 0;
        }
        return realRegFee;
    }

    public void setRealRegFee(Integer realRegFee) {
        this.realRegFee = realRegFee;
    }

    public Integer getRealTreatFee() {
        if (realTreatFee == null) {
            realTreatFee = 0;
        }
        return realTreatFee;
    }

    public void setRealTreatFee(Integer realTreatFee) {
        this.realTreatFee = realTreatFee;
    }

    public RegisterRecord() {
        super();
    }

    public RegisterRecord(String orderNo) {
        super(orderNo);
    }

    public RegisterRecord(Integer regStatus, Integer payStatus, Integer cardType, String cardNo, Long registerTime, String registerDate,
                          Integer isException, Integer isHandleSuccess, Integer handleCount, String handleLog, Integer regType, Long payTimeoutTime,
                          Date scheduleDate, Integer timeFlag, Date beginTime, Date endTime, Integer regPersonType, Integer regMode, String serialNum,
                          String visitLocation, String barcode, String visitDesc, String diseaseDesc, Integer onlinePaymentType, Integer waitPayTime,
                          Integer isValid) {
        super();
        this.regStatus = regStatus;
        this.payStatus = payStatus;
        this.setCardType(cardType);
        this.setCardNo(cardNo);
        this.registerTime = registerTime;
        this.registerDate = registerDate;
        this.isException = isException;
        this.isHandleSuccess = isHandleSuccess;
        this.handleCount = handleCount;
        this.handleLog = handleLog;
        this.regType = regType;
        this.payTimeoutTime = payTimeoutTime;
        this.scheduleDate = scheduleDate;
        this.timeFlag = timeFlag;
        this.beginTime = beginTime;
        this.endTime = endTime;
        this.regPersonType = regPersonType;
        this.regMode = regMode;
        this.serialNum = serialNum;
        this.visitLocation = visitLocation;
        this.barcode = barcode;
        this.visitDesc = visitDesc;
        this.diseaseDesc = diseaseDesc;
        this.onlinePaymentType = onlinePaymentType;
        this.waitPayTime = waitPayTime;
        this.isValid = isValid;
    }

    public Integer getIsValid() {
        return isValid;
    }

    public void setIsValid(Integer isValid) {
        this.isValid = isValid;
    }

    public Integer getRegStatus() {
        return regStatus;
    }

    public void setRegStatus(Integer regStatus) {
        this.regStatus = regStatus;
    }

    public Integer getIsHandleSuccess() {
        return isHandleSuccess;
    }

    public void setIsHandleSuccess(Integer isHandleSuccess) {
        this.isHandleSuccess = isHandleSuccess;
    }

    public Integer getHandleCount() {
        return handleCount;
    }

    public void setHandleCount(Integer handleCount) {
        this.handleCount = handleCount;
    }

    public String getHandleLog() {
        return handleLog;
    }

    public void setHandleLog(String handleLog) {
        this.handleLog = handleLog == null ? null : handleLog.trim();
    }

    public Integer getRegType() {
        return regType;
    }

    public void setRegType(Integer regType) {
        this.regType = regType;
    }

    public Long getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Long registerTime) {
        this.registerTime = registerTime;
    }

    public Long getPayTimeoutTime() {
        return payTimeoutTime;
    }

    public void setPayTimeoutTime(Long payTimeoutTime) {
        this.payTimeoutTime = payTimeoutTime;
    }

    public Date getScheduleDate() {
        return scheduleDate;
    }

    public void setScheduleDate(Date scheduleDate) {
        this.scheduleDate = scheduleDate;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getRegMode() {
        return regMode;
    }

    public void setRegMode(Integer regMode) {
        this.regMode = regMode;
    }

    public String getSerialNum() {
        return serialNum == null ? "请到医院后自行取号" : serialNum;
    }

    public void setSerialNum(String serialNum) {
        this.serialNum = serialNum;
    }

    public String getVisitLocation() {
        return visitLocation == null ? "请到院后咨询前台" : visitLocation;
    }

    public void setVisitLocation(String visitLocation) {
        this.visitLocation = visitLocation == null ? null : visitLocation.trim();
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode == null ? null : barcode.trim();
    }

    public String getVisitDesc() {
        return visitDesc;
    }

    public void setVisitDesc(String visitDesc) {
        this.visitDesc = visitDesc == null ? null : visitDesc.trim();
    }

    public Integer getRegPersonType() {
        return regPersonType;
    }

    public void setRegPersonType(Integer regPersonType) {
        this.regPersonType = regPersonType;
    }

    public String getDiseaseDesc() {
        return diseaseDesc;
    }

    public void setDiseaseDesc(String diseaseDesc) {
        this.diseaseDesc = diseaseDesc;
    }

    public Integer getTimeFlag() {
        return timeFlag;
    }

    public void setTimeFlag(Integer timeFlag) {
        this.timeFlag = timeFlag;
    }

    public Integer getWaitPayTime() {
        if (waitPayTime == null) {
            if (payTimeoutTime != null) {
                waitPayTime = (int) ((payTimeoutTime - registerTime) / 60000);
            } else {
                waitPayTime = 0;
            }
        }
        return waitPayTime;
    }

    public void setWaitPayTime(Integer waitPayTime) {
        this.waitPayTime = waitPayTime;
    }

    public Integer getOnlinePaymentType() {
        return onlinePaymentType;
    }

    public void setOnlinePaymentType(Integer onlinePaymentType) {
        this.onlinePaymentType = onlinePaymentType;
    }

    public String getRegisterDate() {
        if (registerTime != null) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTimeInMillis(registerTime);
            registerDate = BizConstant.YYYYMMDDHHMMSS.format(cal.getTime());
        }
        return registerDate;
    }

    public void setRegisterDate(String registerDate) {
        this.registerDate = registerDate;
    }

    public Integer getIsException() {
        return isException;
    }

    public void setIsException(Integer isException) {
        this.isException = isException;
    }

    public void setScheduleDateLable(String scheduleDateLable) {
        this.scheduleDateLable = scheduleDateLable;
    }

    public Double getRefundFee() {
        refundFee = Double.valueOf(getRealRegFee() + getRealTreatFee()) / 100;
        return refundFee;
    }

    public void setRefundFee(Double refundFee) {
        this.refundFee = refundFee;
    }

    @Override
    public Integer getPayTotalFee() {
        return getRealRegFee() + getRealTreatFee();
    }

    @Override
    public Integer getRefundTotalFee() {
        return getRealRegFee() + getRealTreatFee();
    }

    public Integer getIsPay() {
        return isPay;
    }

    public void setIsPay(Integer isPay) {
        this.isPay = isPay;
    }

    /**
     * 转化为订单信息
     *
     * @return public RegisterOrder convertRegisterOrder() {
     * RegisterOrder order = new RegisterOrder();
     * order.setOrderNo(this.orderNo);
     * order.setOrderNoHashVal(this.orderNoHashVal);
     * order.setAppId(appId);
     * order.setAppCode(appCode);
     * <p>
     * //业务名:科室-医生名称
     * String orderTitle = BizConstant.BIZ_TYPE_REGISTER_NAME.concat(":").concat(this.deptName).concat("-").concat(doctorName);
     * order.setOrderTitle(orderTitle);
     * order.setCardType(this.cardType);
     * order.setCardNo(this.cardNo);
     * order.setRegType(regType);
     * <p>
     * order.setOpenId(this.openId);
     * order.setBusinessCode(BizConstant.BIZ_TYPE_REGISTER);
     * order.setBusinessName(BizConstant.BIZ_TYPE_REGISTER_NAME);
     * <p>
     * order.setHospitalId(this.hospitalId);
     * order.setBranchId(branchHospitalId);
     * <p>
     * String agtCode = null;
     * switch (regMode) {
     * case BizConstant.MODE_TYPE_WEIXIN_VAL:
     * agtCode = BizConstant.MODE_TYPE_WEIXIN;
     * break;
     * case BizConstant.MODE_TYPE_ALIPAY_VAL:
     * agtCode = BizConstant.MODE_TYPE_ALIPAY;
     * break;
     * default:
     * agtCode = BizConstant.MODE_TYPE_OTHER;
     * break;
     * }
     * order.setHisOrdNum(hisOrdNo);
     * order.setAgtCode(agtCode);
     * order.setOrderTime(new Date(registerTime));
     * order.setWaitPayTime(waitPayTime);
     * order.setOnlinePaymentType(onlinePaymentType);
     * order.setPayMode(this.regMode);
     * order.setPayAmout( ( this.realRegFee == null ? 0 : this.realRegFee ) + ( this.realTreatFee == null ? 0 : this.realTreatFee ));
     * return order;
     * }
     */


    public String getScheduleDateLable() {
        if (scheduleDate != null) {
            scheduleDateLable = BizConstant.YYYYMMDD.format(scheduleDate);
        }
        if (scheduleDateLable != null && beginTime != null) {
            scheduleDateLable += " ";
            scheduleDateLable = scheduleDateLable.concat(BizConstant.HHMM.format(beginTime));
        }
        if (scheduleDateLable != null && endTime != null) {
            scheduleDateLable += "-";
            scheduleDateLable = scheduleDateLable.concat(BizConstant.HHMM.format(endTime));
        }
        return scheduleDateLable;
    }

    public String getTradeType() {
        return tradeType;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public String getTradeTime() {
        return tradeTime;
    }

    public void setTradeTime(String tradeTime) {
        this.tradeTime = tradeTime;
    }

    public String getWorkId() {
        return workId;
    }

    public void setWorkId(String workId) {
        this.workId = workId;
    }

    private String isInsurance;
    private String accountAmount;
    private String medicareAmount;
    private String insuranceAmount;
    /**
     * 参保类型
     * 当订单为医保挂号订单时必输
     * 1：基本医疗保险（一档）
     * 2：基本医疗保险（二档）
     * 3：特殊
     * 4：基本医疗保险（三档）
     * 5：医疗保险二档（少儿）
     * 6：统筹保险
     */
    private String insuredType;
    /**
     * 病人类型
     * 当订单为医保挂号订单时必输
     * 1：医疗保险
     * 2：生育医疗
     * 3：离休医疗
     * 4：家属统筹医疗
     * 5：工伤医疗
     * 6：农民工医疗
     */
    private String patientType;

    private String paymentId;
    private String paymentFee;

    @Override
    public String getIsInsurance() {
        return isInsurance;
    }

    @Override
    public void setIsInsurance(String isInsurance) {
        this.isInsurance = isInsurance;
    }

    public String getAccountAmount() {
        return accountAmount;
    }

    public void setAccountAmount(String accountAmount) {
        this.accountAmount = accountAmount;
    }

    public String getMedicareAmount() {
        return medicareAmount;
    }

    public void setMedicareAmount(String medicareAmount) {
        this.medicareAmount = medicareAmount;
    }

    public String getInsuranceAmount() {
        return insuranceAmount;
    }

    public void setInsuranceAmount(String insuranceAmount) {
        this.insuranceAmount = insuranceAmount;
    }

    public String getPaymentId() {
        return paymentId;
    }

    public void setPaymentId(String paymentId) {
        this.paymentId = paymentId;
    }

    public String getPaymentFee() {
        return paymentFee;
    }

    public void setPaymentFee(String paymentFee) {
        this.paymentFee = paymentFee;
    }

    /**
     * 医院编号
     */
    private String yljgbm;
    /**
     * 操作员编码
     */
    private String czybm;
    /**
     * 操作员姓名
     */
    private String czyxm;

    public String getYljgbm() {
        return yljgbm;
    }

    public void setYljgbm(String yljgbm) {
        this.yljgbm = yljgbm;
    }

    public String getCzybm() {
        return czybm;
    }

    public void setCzybm(String czybm) {
        this.czybm = czybm;
    }

    public String getCzyxm() {
        return czyxm;
    }

    public void setCzyxm(String czyxm) {
        this.czyxm = czyxm;
    }

    /**
     * 医保挂号流水号(自费为空)
     */
    private String medicalRegisterSerialNum;

    public String getMedicalRegisterSerialNum() {
        return medicalRegisterSerialNum;
    }

    public void setMedicalRegisterSerialNum(String medicalRegisterSerialNum) {
        this.medicalRegisterSerialNum = medicalRegisterSerialNum;
    }

    public MessagePushParamsVo convertMessagePushParams() {
        MessagePushParamsVo params = new MessagePushParamsVo();
        params.setHospitalId(this.hospitalId);
        params.setAppId(this.appId);

        params.setOpenId(this.openId.replaceAll(" ", "+"));
        if (regMode != null) {
            if (regMode.intValue() == BizConstant.MODE_TYPE_WEIXIN_VAL) {
                params.setPlatformType(BizConstant.MODE_TYPE_WEIXIN);
            } else if (regMode.intValue() == BizConstant.MODE_TYPE_ALIPAY_VAL) {
                params.setPlatformType(BizConstant.MODE_TYPE_ALIPAY);
            } else if (regMode.intValue() == BizConstant.MODE_TYPE_EASYHEALTH_ALIPAY_VAL
                    || regMode.intValue() == BizConstant.MODE_TYPE_EASYHEALTH_WEIXIN_VAL
                    || regMode.intValue() == BizConstant.MODE_TYPE_EASYHEALTH_UNIONPAY_VAL) {
                params.setPlatformType(BizConstant.MODE_TYPE_EASYHEALTH);
            }
        } else {
            params.setPlatformType(BizConstant.MODE_TYPE_WEIXIN);
        }

        Map<String, Serializable> dataMap = Jackson.getBeanJsonData(Jackson.getBaseJsonData(this), Map.class);

        String urlParms =
                BizConstant.URL_PARAM_CHAR_FIRST.concat(BizConstant.URL_PARAM_ORDER_NO).concat(BizConstant.URL_PARAM_CHAR_ASSGIN)
                        .concat(this.orderNo).concat(BizConstant.URL_PARAM_CHAR_CONCAT).concat(BizConstant.URL_PARAM_OPEN_ID)
                        .concat(BizConstant.URL_PARAM_CHAR_ASSGIN).concat(this.openId).concat(BizConstant.URL_PARAM_CHAR_CONCAT)
                        .concat(BizConstant.URL_PARAM_APPID).concat(BizConstant.URL_PARAM_CHAR_ASSGIN).concat(this.appId)
                        .concat(BizConstant.URL_PARAM_CHAR_CONCAT).concat(BizConstant.URL_PARAM_APPCODE).concat(BizConstant.URL_PARAM_CHAR_ASSGIN)
                        .concat(this.appCode);
        dataMap.put(BizConstant.MSG_PUSH_URL_PARAMS_KEY, urlParms);
        params.setParamMap(dataMap);
        return params;
    }

    private String tableName;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

}